<template>
	<view>
		<s-layout title="商品库存" navbar="custom1" :bgStyle="{ color: '#F1F1F1' }">
			<assNavbar class="ss-m-b-10" title="扫码核销"  
			:background="{img: 'none',BGcolor: ' linear-gradient(180deg, #7881d9 0%, #f0b8ea 100%)'}"  
			></assNavbar>
			<view class="text-center">
				<image
				  :src="sheep.$url.cdn('/static/merchant/shop/icon-scan.png')"
				  class="scan-btn ss-m-y-60"
				  @tap="openscan"
				></image>
			</view>
			<su-sticky bgColor="#fff">
				<view class="bg-white ss-flex ss-p-y-10 ss-p-x-20">
					<view class="icon"></view>
					<view class="font-1 ss-m-l-20">核销记录</view>
				</view>
			</su-sticky>
			
			
			
				<view class="ss-p-20 ss-p-b-40">
					<view
					  class="log-item bg-white ss-p-y-10 ss-p-x-20"
					  v-for="(item,index) in state.list"
					  :key="index"
					>
					    <view class="ss-flex justify-between item">
							 <view>优惠券ID</view>
							 <view>{{item.code}}</view>
						 </view>
						 <view class="ss-flex justify-between item">
							 <view>核销用户</view>
							 <view>{{item.user?.nickname}}</view>
						 </view>
						 <view class="ss-flex justify-between item">
							 <view>购买时间</view>
							 <view>{{item.create_time}}</view>
						 </view>
						 <view class="ss-flex justify-between item">
							 <view>核销时间</view>
							 <view>{{item.use_time}}</view>
						 </view>
					</view>
				</view>
		</s-layout>
	</view>
</template>

<script setup>
//附近门店列表页
import {
		computed,
		reactive,
		onBeforeMount,
		ref
	} from 'vue';
import {
	onLoad,
	onReachBottom,
	onPageScroll,
	onReady
} from '@dcloudio/uni-app';
import sheep from '@/sheep';
import _ from 'lodash'
import assNavbar from '@/components/ass-navbar.vue'
import {
		queryToParams
	} from '@/sheep/helper/index.js';
const sys_navBar = sheep.$platform.navbar;//标题栏高度
const windowHeight = sheep.$platform.device.windowHeight; // 获得窗口高度

const state = reactive({
	list:[],
	page:0,
	last_page:1
})

// 核销
function openscan() {
	  // #ifdef H5 || MP
	  // 扫码格式  冒号分割的字符串  "keyName:value"
	  uni.scanCode({
	    success: async function (res) {
		  // 团购绑定人员
		  if (res.result.indexOf('bind') !== -1) {
		  	const obj = res.result.split('&').reduce((acc, item) => {
		  	  const [key, value] = item.split('=');
		  	  acc[key] = decodeURIComponent(value);
		  	  return acc;
		  	}, {});
		  	return sheep.$router.go('/pages/merchant/bindCheck',{
		  		id:obj.store_id,
		  		code:obj.code
		  	})
		  }
		  
		  // 团购核销
		  if (res.result.indexOf('code') !== -1) {
		  	const obj = res.result.split('&').reduce((acc, item) => {
		  	  const [key, value] = item.split('=');
		  	  acc[key] = decodeURIComponent(value);
		  	  return acc;
		  	}, {});
		  	
		  	const {
		  		error,
		  		data
		  	} = await sheep.$api.merchant.checkCode({
		  		code: obj.code
		  	})
		  	
		  	if (error === 0) {
		  		uni.showModal({
		  			title: '提示',
		  			content: '核销成功！',
		  			success: function(res) {
		  			}
		  		});
		  	}
		  	
		  	return 
		  }
		  
	    },
		fail: (err) => {
			console.log('--------',err)
		}
	  });
	  // #endif
	  // #ifdef APP
	  showAuditModal(
	    'WRITE_EXTERNAL_STORAGE,CAMERA',
	    () => {
	      uni.scanCode({
	        success: async function (res) {
	          let data = res.result.split(':');
	
	          if (data.length == 2) {
	            try {
	              switch (data[0]) {
	                case 'verify_code': {
	                  let res = await sheep.$api.merchant.verify({
	                    verify_code: data[1],
	                  });
	
	                  if (!res.error) {
	                    // 核销成功
	                    uni.showToast({
	                      icon: 'success',
	                      title: res.msg,
	                    });
	                  }
	                  break;
	                }
	                case 'https':
	                  console.log(res.result);
	                  plus.runtime.openURL(res.result);
	                  break;
	                case 'http':
	                  console.log(res.result);
	                  plus.runtime.openURL(res.result);
	                  break;
	                default:
	                  console.log(data[0]);
	                  break;
	              }
	            } catch (e) {
	              console.log(e);
	            }
	          }
	        },
	      });
	    },
	    null,
	    false,
	  );
	  // #endif
	}
// 核销记录
async function log(page){
	
	if(state.last_page>state.page){
		state.page=state.page+1
		if(page){
			state.page=page
		}
		const res = await sheep.$api.merchant.tuangou_log_hx({
			page:state.page, 
		});
		if(res.error==0){
			state.last_page=res.data.last_page
			if(state.page==1){
				state.list=res.data.data
			}else{
				state.list=state.list.concat(res.data.data)
			}
		}
	}
	
}
// 上拉加载更多
onReachBottom(async () => {
    await log()
  });
onLoad(async ()=>{
	await log()
})
</script>


<style lang="scss" scoped>
	view{
		box-sizing: border-box;
		font-family: Source Han Sans; 
	}
	
	
	
	.font-w{
		font-weight: bold;
	}
	.scan-btn{
		width:400rpx;
		height:400rpx;
		border-radius: 50%;
		
	}
	.icon{
		width: 6rpx;
		height: 40rpx;
		
		/* 主题色 */
		background: linear-gradient(180deg, #7881D9 0%, #F0B8EA 100%);
	}
	.log-item{
		font-size: 28rpx;
		border-radius:20rpx;
		color: #3D3D3D;
		margin-bottom: 20rpx;
		.item{
			padding:10rpx;
			border-bottom: 2rpx solid #f1f1f1;
		}
	}
</style>
